<?php
require_once('MySqlConnection.php');
require_once('District.php'); 

class DistrictDAL{
	private $_connection;
	//Singleton pattern
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new DistrictDAL();
		return self::$_instance;
	}
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getDistricts(){
		$this->_connection->Open();
		$sqlString = "select * from district";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'district');
	}
	
	public function getDistrictsByCountry($id){
		$this->_connection->Open();
		$sqlString = "select * from district where country_id = '{$id}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'district');
	}
	
	public function addDistrict($district){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
	    if($stmt = $mysqli->prepare("INSERT INTO district(district_name, country_id, maparea) VALUES(?,?,?)")) {
			$stmt->bind_param("s", $district['district_name'], $district['country_id'], $district['maparea']);
			$success = $stmt->execute();
			if(!$success){
				echo "Inserting district failed!";
			}
		}
		else{
			
			echo "Failed preparing insert district statement!";
		}
		$mysqli -> close();
	}
	
	public function removeDistrict($districtId){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("DELETE FROM district WHERE district_id = ?")) {
			$stmt->bind_param("i", $districtId);
			$success = $stmt->execute();
			if(!$success){
				echo "Removing district failed!";
			}
		}
		else{
			echo "Failed preparing delete district statement!";
		}
		$mysqli -> close();
	}
	
	public function updateDistrict($district) {
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("UPDATE district SET district_name = ?, country_id = ?, maparea = ? WHERE district_id = ?")) {
			$stmt->bind_param("sisi", $district['district_name'], $district['country_id'],$district['maparea'], $district['id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Updating district failed!";
			}
		}
		else{
			echo "Failed preparing update district statement!";
		}
		$mysqli -> close();
	}
	
	//MAPPERS
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new District($data->district_id,$data->district_name, $data->country_id, $data->maparea);
		return $object;
	}
}